package mest.service.hws.impl;

import com.alibaba.fastjson.JSON;
import com.sun.xml.bind.v2.model.core.ID;
import mest.entity.warehouse_purchase;
import mest.service.cxj.DictionariesOrderService;
import mest.service.hws.warehouse_productjob_purchaseService;
import mest.service.hws.warehouse_purchaseService;

import javax.annotation.Resource;
import javax.persistence.Id;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import mest.dao.hws.warehouse_purchaseMapper;
import org.springframework.stereotype.Service;

@Service
public class warehouse_purchaseServiceImpl implements warehouse_purchaseService {

    @Resource
    private warehouse_purchaseMapper warehouse_purchaseMapper;

    @Resource
    private DictionariesOrderService dno;

    @Resource
    private warehouse_productjob_purchaseService warehouse_productjob_purchaseService;

    @Override
    public String insert(String theirDb,
                         List<Map<String,Object>> record) {
        /*/insertModel /delModel /updateByone*/
        System.out.println("进入增删改方法");
        //采购单生成
        Map<String,Object> map1 = record.get(0);
        Map<String, Object> map = new HashMap<String, Object>();
        try {//自动生成流水号并set入对象
            System.out.println(dno.selectDictionariesOrder(theirDb).getPlanPurchaseNumber());
            map1.put("tableNumber",("CKLCGD-"+dno.selectDictionariesOrder(theirDb).getPlanPurchaseNumber()));
            map1.put("theirDb",theirDb);
            /* map1.put("id",0);*/
            System.out.println("调用采购单新增方法");
            warehouse_purchaseMapper.insert(map1);
            System.out.println("生成仓库流采购单"+map1);

            //采购单生成后返回数据
            //数据的id放进对象数组，调用内容表插入方法
            for (Map<String,Object> map2:record){
                map2.put("wpId",map1.get("id"));
                map2.put("theirDb",map1.get("theirDb"));
                //入库数量为操作记录入库数量，thisnumber
                map2.put("planNumber",map2.get("thisNumber"));
            }
            System.out.println("调用采购内容单新增方法");
            warehouse_productjob_purchaseService.insert(theirDb,record);
            System.out.println("调用采购内容单新增方法");
            //操作成功后字典表+1
            dno.add_dictionaries_Order(theirDb,"planPurchaseNumber");

            map.put("code", 0);
            map.put("msg", "操作成功");
            map.put("count",(record==null)?0:1 );
        }catch (Exception e){
            map.put("code", 1);
            map.put("msg", "操作失败");
            e.toString();
        }    
        return JSON.toJSONString(map);
    }

    @Override
    public String updateByPrimaryKey(String theirDb,
                                     warehouse_purchase record) {
        return null;
    }

    @Override
    public String selectByPrimaryKey(String theirDb,
                                     Integer id, Integer finishStatus) {
        return null;
    }

    @Override
    public Map<Object, Object> selectPrimary(String theirDb,
                                             Integer limit, Integer page, Integer finishStatus) {
        return null;
    }
}
